//
//  FSSwitchMenuCommon.m
//  cocoapodText
//
//  Created by apple on 16/8/13.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "ShiPei.h"
#import "FSSwitchMenuCommon.h"
#import <Foundation/Foundation.h>
@interface FSSwitchMenuCommon()
@end

@implementation FSSwitchMenuCommon
-(instancetype)initWithFrame:(CGRect)frame{
    self=[super initWithFrame:frame];
    if (self) {
        //自动伸缩高度可伸缩，右边距可以伸缩
        self.autoresizingMask=UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleRightMargin;
        //设置背景图片
        [self setBackgroundColor:[UIColor whiteColor]];
        [self addTabItems];
    }
    return self;
}

-(void)setFrame:(CGRect)frame{
    frame.size.height=[ShiPei CGFloatMakeScale:FSCommonMenuH];
    [super setFrame:frame];
    CGRect temFrame=self.TabItem1.itemNameLabel.frame;
    temFrame.size.width=temFrame.size.width+[ShiPei CGFloatMakeScale:30];
    temFrame.size.height=temFrame.size.height*2;
    CGRect item1Frame=temFrame;
    temFrame=self.TabItem2.itemNameLabel.frame;
    temFrame.size.width=temFrame.size.width+[ShiPei CGFloatMakeScale:30];
    temFrame.size.height=temFrame.size.height*2;
    CGRect item2Frame=temFrame;
    temFrame=self.TabItem3.itemNameLabel.frame;
    temFrame.size.width=temFrame.size.width+[ShiPei CGFloatMakeScale:30];
    temFrame.size.height=temFrame.size.height*2;
    CGRect item3Frame=temFrame;
    temFrame=self.TabItem4.itemNameLabel.frame;
    temFrame.size.width=temFrame.size.width+[ShiPei CGFloatMakeScale:30];
    temFrame.size.height=temFrame.size.height*2;
    CGRect item4Frame=temFrame;
    [self.TabItem1 setFrame:item1Frame];
    [self.TabItem2 setFrame:item2Frame];
    [self.TabItem3 setFrame:item3Frame];
    [self.TabItem4 setFrame:item4Frame];
    [self.TabItem1 setCenter:[ShiPei CGPointMakeScale:frame.size.width-(item1Frame.size.width/2) :[ShiPei CGFloatMakeScale:(FSCommonMenuH/2)]]];
    [self.TabItem2 setCenter:[ShiPei CGPointMakeScale:frame.size.width-item1Frame.size.width-(item2Frame.size.width/2) :[ShiPei CGFloatMakeScale:(FSCommonMenuH/2)]]];
    [self.TabItem3 setCenter:[ShiPei CGPointMakeScale:frame.size.width-item1Frame.size.width-item2Frame.size.width-(item3Frame.size.width/2) :[ShiPei CGFloatMakeScale:(FSCommonMenuH/2)]]];
    [self.TabItem4 setCenter:[ShiPei CGPointMakeScale:frame.size.width-item1Frame.size.width-item2Frame.size.width-item3Frame.size.width-(item4Frame.size.width/2) :[ShiPei CGFloatMakeScale:(FSCommonMenuH/2)]]];
}

//添加Tab选项卡
- (void)addTabItems
{
    [self addSingleTabWithWeight:1];
    [self addSingleTabWithWeight:2];
    [self addSingleTabWithWeight:3];
    [self addSingleTabWithWeight:4];
    [self changeTabItemStyle:[self.TabItem4 tag]];
}

-(void)setTabItem1Text:(NSString *)tabItem1Text{
    [self.TabItem1 setLabelText:tabItem1Text];
}

-(void)setTabItem2Text:(NSString *)tabItem2Text{
    [self.TabItem2 setLabelText:tabItem2Text];
}

-(void)setTabItem3Text:(NSString *)tabItem3Text{
    [self.TabItem3 setLabelText:tabItem3Text];
}

-(void)setTabItem4Text:(NSString *)tabItem4Text{
    [self.TabItem4 setLabelText:tabItem4Text];
}

- (void)addSingleTabWithWeight:(int)weight
{
    FSMenuTabCommon *tabItem=[[FSMenuTabCommon alloc]initWithFrame:CGRectMake(0, 0,0, [ShiPei CGFloatMakeScale:FSCommonMenuH])];
    
    tabItem.userInteractionEnabled=YES;
    UITapGestureRecognizer *click =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tabItemTouchEvent:)];
    [tabItem addGestureRecognizer:click];
    tabItem.tag = weight - 1;
    [self addSubview:tabItem];
    switch (tabItem.tag) {
        case 0:
            self.TabItem1=tabItem;
            break;
        case 1:
            self.TabItem2=tabItem;
            break;
        case 2:
            self.TabItem3=tabItem;
            break;
        case 3:
            self.TabItem4=tabItem;
            break;
        default:
            break;
    }
}

//设置触摸事件
- (void)tabItemTouchEvent:(id)sender{
    UITapGestureRecognizer *singleTap = (UITapGestureRecognizer *)sender;
    int tabTag=[[NSString stringWithFormat: @"%ld", (long)[singleTap view].tag] intValue];
    int menuTag=[[NSString stringWithFormat:@"%ld",(long)self.tag]  intValue];
    if ([self.menuDelegate respondsToSelector:@selector(switchMenuCommonByTabItem:menuId:destinationTab:)]) {
        [self.menuDelegate switchMenuCommonByTabItem:self menuId:menuTag destinationTab:tabTag];
    }
    [self changeTabItemStyle:tabTag];
}

-(void)changeTabItemStyle:(NSInteger)tag{
    switch (tag) {
        case 0:
            [self.TabItem1.itemNameLabel setTextColor:FUIColorFromRGB(0x1b75cc)];
            [self.TabItem2.itemNameLabel setTextColor:FUIColorFromRGB(0x999999)];
            [self.TabItem3.itemNameLabel setTextColor:FUIColorFromRGB(0x999999)];
            [self.TabItem4.itemNameLabel setTextColor:FUIColorFromRGB(0x999999)];
            [self.TabItem1.itemUnderlineImage setBackgroundColor:FUIColorFromRGB(0xff5e22)];
            [self.TabItem2.itemUnderlineImage setBackgroundColor:[UIColor clearColor]];
            [self.TabItem3.itemUnderlineImage setBackgroundColor:[UIColor clearColor]];
            [self.TabItem4.itemUnderlineImage setBackgroundColor:[UIColor clearColor]];
            break;
        case 1:
            [self.TabItem2.itemNameLabel setTextColor:FUIColorFromRGB(0x1b75cc)];
            [self.TabItem1.itemNameLabel setTextColor:FUIColorFromRGB(0x999999)];
            [self.TabItem3.itemNameLabel setTextColor:FUIColorFromRGB(0x999999)];
            [self.TabItem4.itemNameLabel setTextColor:FUIColorFromRGB(0x999999)];
            [self.TabItem2.itemUnderlineImage setBackgroundColor:FUIColorFromRGB(0xff5e22)];
            [self.TabItem1.itemUnderlineImage setBackgroundColor:[UIColor clearColor]];
            [self.TabItem3.itemUnderlineImage setBackgroundColor:[UIColor clearColor]];
            [self.TabItem4.itemUnderlineImage setBackgroundColor:[UIColor clearColor]];
            break;
        case 2:
            [self.TabItem3.itemNameLabel setTextColor:FUIColorFromRGB(0x1b75cc)];
            [self.TabItem2.itemNameLabel setTextColor:FUIColorFromRGB(0x999999)];
            [self.TabItem1.itemNameLabel setTextColor:FUIColorFromRGB(0x999999)];
            [self.TabItem4.itemNameLabel setTextColor:FUIColorFromRGB(0x999999)];
            [self.TabItem3.itemUnderlineImage setBackgroundColor:FUIColorFromRGB(0xff5e22)];
            [self.TabItem2.itemUnderlineImage setBackgroundColor:[UIColor clearColor]];
            [self.TabItem1.itemUnderlineImage setBackgroundColor:[UIColor clearColor]];
            [self.TabItem4.itemUnderlineImage setBackgroundColor:[UIColor clearColor]];
            break;
        case 3:
            [self.TabItem4.itemNameLabel setTextColor:FUIColorFromRGB(0x1b75cc)];
            [self.TabItem2.itemNameLabel setTextColor:FUIColorFromRGB(0x999999)];
            [self.TabItem3.itemNameLabel setTextColor:FUIColorFromRGB(0x999999)];
            [self.TabItem1.itemNameLabel setTextColor:FUIColorFromRGB(0x999999)];
            [self.TabItem4.itemUnderlineImage setBackgroundColor:FUIColorFromRGB(0xff5e22)];
            [self.TabItem2.itemUnderlineImage setBackgroundColor:[UIColor clearColor]];
            [self.TabItem3.itemUnderlineImage setBackgroundColor:[UIColor clearColor]];
            [self.TabItem1.itemUnderlineImage setBackgroundColor:[UIColor clearColor]];
            break;
        default:
            break;
    }
}
@end
